package com.ibeeking.found.msg.rest.controller.v1;

import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.enums.sms.SmsTypeCodeEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.msg.rest.service.ISendSmsService;
import com.ibeeking.found.msg.service.common.param.MsgSendParam;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.annotation.Log;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.alibaba.nacos.common.http.param.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;

/**
 * @ClassName SmsRelationController控制器
 * @Description 发送消息
 * @Author ibeeking
 * @Date 2020-12-09 10:54
 **/
@RestController
@RequestMapping("/msg/sendSms/v1")
public class SendSmsController {

    @Resource
    private ISendSmsService sendSmsServiceImpl;

    @IgnoreDS
    @Log(logType = OperateLogTypeEnum.SAVE, describe = "发送登录验证码")
    @GetMapping(value = "/loginCheck/{mobile}", produces = MediaType.APPLICATION_JSON)
    public String loginCheck(@PathVariable("mobile") @NotBlank(message = "手机号不能为空") String mobile) {
        MsgSendParam param = new MsgSendParam();
        param.setMobile(mobile);
        param.setSmsTypeCode(SmsTypeCodeEnum.login_msg_type);
        return sendSmsServiceImpl.verificationCode(-1, param);
    }

    @IgnoreDS
    @Log(logType = OperateLogTypeEnum.SAVE, describe = "发送修改密码验证码")
    @GetMapping(value = "/modifyPwd/{mobile}", produces = MediaType.APPLICATION_JSON)
    public String modifyPwd(@PathVariable("mobile") @NotBlank(message = "手机号不能为空") String mobile) {
        MsgSendParam param = new MsgSendParam();
        param.setMobile(mobile);
        param.setSmsTypeCode(SmsTypeCodeEnum.modify_pwd_check_type);
        return sendSmsServiceImpl.verificationCode(-1, param);
    }

    @Log(logType = OperateLogTypeEnum.SAVE, describe = "发送身份验证码")
    @PostMapping(value = "/verificationCode", produces = MediaType.APPLICATION_JSON)
    public String verificationCode(HttpServletRequest request, @RequestBody @Validated MsgSendParam param) {
        String tenantId = request.getHeader(HeaderNounConstant.TENANT_ID);
        if (StringUtils.isBlank(tenantId)) {
            throw new BusinessException("租户必填");
        }
        return sendSmsServiceImpl.verificationCode(Integer.valueOf(tenantId), param);
    }
}
